<template>
	<view>
		<mescroll-body ref="mescrollRef" @init="mescrollInit" @up="upCallback" :up="upOption"  @down="downCallback">
			<view class="p-l-20 p-r-20 p-t-34">
				<address-book v-for="(item,index) in list" :select="select" :item="item" @del="delAddressFun" ></address-book>
			</view>
		</mescroll-body>
		<router-link class="footer-con" :to="{path: '/bundle/pages/address_book_add/address_book_add'}">
			<view class="footer-btn bg-primary white md flex row-center">添加地址</view>
		</router-link>
	</view>
</template>

<script>
	import { getAddressLists, delAddress, setDefaultAddress } from '@/api/user';
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				upOption: {
					empty: {
						icon: '/static/images/order_null.png',
						tip: '暂无记录', // 提示
					}
				},
				list:[],
				select:0
			}
		},
		onLoad({select}) {
			if(!!select){
				this.select = select;
			}
			uni.$on('update',()=>{
				this.downCallback();
			})
		},
		onUnload(){
			uni.$off('update')
		},
		methods: {
			delAddressFun(id){
				delAddress(id).then(res=>{
					if(res.code==1){
						this.$toast({
							title:'删除成功'
						})
						this.downCallback();
					}
				})
			},
			upCallback(page){
				const pageNum = page.num; // 页码, 默认从1开始
				const pageSize = page.size; // 页长, 默认每页10条
				getAddressLists({
					page:pageNum,
					limit:pageSize,
			
				}).then(({code,data})=>{
					if(code==1){
						if (page.num == 1) this.list = [];
						// console.log(page.num);
						const curPageData = data;
						const curPageLen = curPageData.length;
						const count = data.count
						this.list = this.list.concat(curPageData);
						this.mescroll.endBySize(curPageLen,count);
					}
					
				}).catch(err=>{
					console.log(err)
					
					this.mescroll.endErr()
				})
			}
		}
	}
</script>

<style lang="scss">
	.footer-con{
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		box-sizing: content-box;
		padding-bottom: env(safe-area-inset-bottom);
		margin: 0 auto;
		    // width: 100%;
		    padding: 20rpx;
		    background-color: #fff;
		.footer-btn{
			color: #fff;
			height: 80upx;
			width: 716upx ;
			text-align: center;
			line-height: 80upx;
			border-radius: 12upx;
			background-color: #F27A57;
		}
	}
</style>
